from typing import List
import uuid
# import demucs.audio
# import demucs.demucs
# import demucs.separate
from loguru import logger
from pyAudioAnalysis import audioBasicIO as aIO
from pyAudioAnalysis import audioSegmentation as aS
from pathlib import Path
import librosa
# import demucs.api as dapi


# sprtr = dapi.Separator()

def get_audio_segments(path: Path, is_noise_reduction=False):
    # create random tmp file
    [Fs, x] = aIO.read_audio_file(path)
    try:
        segments = aS.silence_removal(x, 
                                    Fs, 
                                    0.020, 
                                    0.020, 
                                    smooth_window=0.4, 
                                    weight=0.3, 
                                    plot=False)
    except Exception as e:
        logger.error(f'got exception {e} for {path}')
        return path, [[0, librosa.get_duration(path=path)]]

    logger.debug(f'going to return segments {segments} for {path}')
    return path, segments

# get_audio_segments(Path('/mnt/d/My Documents/My Games/ZTMZClub/profiles/default/芬兰_Maahi/2450-105.65-638353832314673885.wav'))
# get_audio_segments(Path('/mnt/c/Users/andy/Downloads/default/瑞典_Eksharad/0-0-638353881838505010.wav'))

# def get_audio_slice_tmp_file(audio_path: Path, slice: List):
#     [Fs, x] = aIO.read_audio_file(audio_path)

#     # create random tmp file
#     tmp_file = Path('/tmp/' + str(uuid.uuid4()) + '.wav')

#     # write slice to tmp file
#     aIO.wavfile.write(tmp_file, Fs, x[int(slice[0] * Fs):int(slice[1] * Fs)])
#     # noise_reduction
#     vocals = noise_reduction(tmp_file)
#     dapi.save_audio(vocals, tmp_file, samplerate=sprtr.samplerate)
#     logger.debug(f'got noise reduced file: {tmp_file}')

#     # return tmp file
#     return tmp_file

# def noise_reduction(audio_path: Path):
#     origin, separated = sprtr.separate_audio_file(audio_path)
#     return separated['vocals']


